package Test_2019_2_18;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
 * 4、有五个学生，每个学生有3门课的成绩，从键盘输入以上数据（包括学生号，姓名，三门课成绩）
 * ，计算出平均成绩，将原有的数据和计算出的平均分数存放在磁盘文件"stuScore.txt"中。
 */
public class Test4 {

    /**
     * 获取数据
     */
    public void Scanner() throws IOException {
        Scanner sc1 = new Scanner(System.in);
        System.out.println("请输入学生号");
        int no = sc1.nextInt();
        Scanner sc2 = new Scanner(System.in);
        System.out.println("请输入学生姓名");
        String name = sc2.nextLine();
        Scanner sc3 = new Scanner(System.in);
        System.out.println("请输入学生三门课成绩");
        int a = sc3.nextInt();
        int b = sc3.nextInt();
        int c = sc3.nextInt();
        BufferedWriter(no, name, a, b, c);
    }

    /**
     * 写入stuScore.txt
     */
    public void BufferedWriter(int no, String name, int a, int b, int c) throws IOException {
        //参数为空
        File directory = new File("");
        //当前目录的stuScore.txt文件下
        String courseFile = directory.getCanonicalPath() + "\\" + "stuScore.txt";
        BufferedWriter bw = new BufferedWriter(new FileWriter(courseFile, true));
        //拼接字符串
        String info = "学号：" + no + "," + "姓名：" + name + ","
                + "成绩：" + a + "," + b + "," + c + ","
                + "平均分：" + (a + b + c) / 3;
        //写进去
        bw.write(info);
        //换行
        bw.newLine();
        //刷新
        bw.flush();
        //释放资源
        bw.close();
    }

    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 5; i++) {
            Test4 t = new Test4();
            t.Scanner();
        }
    }
}
